package pe.edu.upc.dai.grupo4.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dai.grupo4.utils.ConnectionUtils;
import pe.edu.upc.dai.grupo4.utils.DaoException;

/**
 *
 * @author tluyo
 */
public class BaseDao <T> {

    public List<T> select(String sql) {
        List<T> result = new ArrayList<T>();
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.prepareStatement(sql);
            /// añade parámetros

            rs = st.executeQuery();
            while (rs.next()) {
                //result.add(this.getData(rs));
            }
        } catch (SQLException e) {
            throw new DaoException(e);
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return result;
    }
    public void setInt(int index, int value) {
        
    }
    public void setString(int index, String value){
        
    }

    protected T getData(ResultSet rs) throws SQLException{
        T result = null;

        return result;
    }

        public int insertar(T t) {
        int result = 0;
        Connection connection = null;
        CallableStatement st = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.prepareCall("------");
            st.registerOutParameter(1, java.sql.Types.INTEGER);
            ///this.comun(st, t);
            st.execute();
            ConnectionUtils.commit(connection);
            result = st.getInt(1);
        } catch (SQLException e) {
            ConnectionUtils.rollback(connection);
            throw new DaoException(e);
        } finally {
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
        return result;
    }

}
